package com.sword.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 常量类
 *
 */
public class TTConstants extends Properties{
	
	private static final long serialVersionUID = -2413773007249715202L;
	
	private static Log log = LogFactory.getLog(TTConstants.class);
	
	public static TTConstants P = null;
	
	static{		
		initProperties();
	}
	
	/**
	 * 载入所有配置文件
	 */
	public static void initProperties() {
		try {
			P = new TTConstants();
			String[] arrProFiles = { "constants.properties"};
			
			for (String proFile : arrProFiles) {
				log.info("load properties file:" + proFile);
				P.load(TTConstants.class.getResourceAsStream("/" + proFile));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 读取配置文件中内容
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String value = "";
		try {
			if (key == null || P == null) {
				return "";
			}
			if (P.containsKey(key))
				value = new String(P.getProperty(key).getBytes("UTF-8"),
						"UTF-8");
			else
				value = "";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return value;
	}
		
	public static void main(String[] args) {
		System.out.println(TTConstants.get("mencached.server1"));
	}	
}
